package com.example.localdate;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalUnit;
import java.util.Date;

public class Main {


    public static void main(String[] args) {
        //本周
        System.out.println("本周");
        test2();
        //上周
        System.out.println("上周");
        test3();
        //本月
        System.out.println("本月");
        test4();
        //上月
        System.out.println("上月");
        test5();

    }

    public void test1(){
        String s = "20190419";
        String y = s.substring(0, 4);
        String m = s.substring(4, 6);
        String d = s.substring(6);
        LocalDate localDate = LocalDate.of(Integer.parseInt(y), Integer.parseInt(m), Integer.parseInt(d));
        LocalDate localDate1 = localDate.plusDays(-1);
        int year = localDate1.getYear();
        int monthValue = localDate1.getMonthValue();
        int dayOfMonth = localDate1.getDayOfMonth();
        DecimalFormat decimalFormat = new DecimalFormat("00");
        System.out.println(year +""+ decimalFormat.format(monthValue) +""+ dayOfMonth);
        String yyyyMMdd = localDate1.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
        System.out.println(yyyyMMdd);
    }

    public static void test2(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime localDateTime = now.with(ChronoField.DAY_OF_WEEK, DayOfWeek.MONDAY.getValue())
                .withHour(0).withMinute(0).withSecond(0);
        Date from = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
        System.out.println(simpleDateFormat.format(from));
        LocalDateTime localDateTime1 = now.with(ChronoField.DAY_OF_WEEK, DayOfWeek.SUNDAY.getValue())
                .withHour(23).withMinute(59).withSecond(59);
        Date from1 = Date.from(localDateTime1.atZone(ZoneId.systemDefault()).toInstant());
        System.out.println(simpleDateFormat.format(from1));
    }

    public static void test3(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime localDateTime = now.plus(-1, ChronoUnit.WEEKS).with(ChronoField.DAY_OF_WEEK, DayOfWeek.MONDAY.getValue())
                .withHour(0).withMinute(0).withSecond(0);
        Date from = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
        System.out.println(simpleDateFormat.format(from));
        LocalDateTime localDateTime1 = now.plus(-1, ChronoUnit.WEEKS).with(ChronoField.DAY_OF_WEEK, DayOfWeek.SUNDAY.getValue())
                .withHour(23).withMinute(59).withSecond(59);
        Date from1 = Date.from(localDateTime1.atZone(ZoneId.systemDefault()).toInstant());
        System.out.println(simpleDateFormat.format(from1));
    }

    public static void test4(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime localDateTime = now.withDayOfMonth(1).withHour(0).withMinute(0).withSecond(0);
        Date from = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
        System.out.println(simpleDateFormat.format(from));
        LocalDateTime localDateTime1 = now.plus(1, ChronoUnit.MONTHS)
                .withDayOfMonth(1).plus(-1, ChronoUnit.DAYS)
                .withHour(23).withMinute(59).withSecond(59);
        Date from1 = Date.from(localDateTime1.atZone(ZoneId.systemDefault()).toInstant());
        System.out.println(simpleDateFormat.format(from1));

    }

    public static void test5(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime localDateTime = now.plus(-1, ChronoUnit.MONTHS)
                .withDayOfMonth(1).withHour(0).withMinute(0).withSecond(0);
        Date from = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
        System.out.println(simpleDateFormat.format(from));
        LocalDateTime localDateTime1 = now.withDayOfMonth(1)
                .plus(-1, ChronoUnit.DAYS)
                .withHour(23).withMinute(59).withSecond(59);
        Date from1 = Date.from(localDateTime1.atZone(ZoneId.systemDefault()).toInstant());
        System.out.println(simpleDateFormat.format(from1));


    }


}
